package com.jwater.console;

import java.io.File;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import com.jwater.core.common.InitLogger;
import com.jwater.core.common.JvmNames;
import com.jwater.core.manager.common.conf.CoreConfParse;
import com.jwater.core.metrics.JvmMetric;
import com.jwater.core.net.NettyClient;
import com.jwater.core.utils.ProcessUtils;
import com.jwater.launch.HomePath;
import com.jwater.launch.Jwater;
import com.jwater.launch.LoaderUtils;

/**
 * @description:启动 main
 * @author: jnner
 * @createDate: 2021/2/5
 * @version: 1.0
 */
@SpringBootApplication
public class ConsoleApp {
    protected static Logger logger = LoggerFactory.getLogger(ConsoleApp.class);

    /**
     * 杀死之前的进程
     */
    public static void killOld() {
        InitLogger.initConsole();
        int currId = JvmMetric.pid();
        logger.info("currId:" + currId);
        CoreConfParse conf = new CoreConfParse();
        conf.parse();
        List<Integer> ids = ProcessUtils.getProcessIds(Jwater.class.getName(),
                JvmNames.getConsoleName(conf.getClusterName()));
        for (Integer id : ids) {
            if (id != currId) {
                try {
                    //强制杀死
                    ProcessUtils.killById(id, true);
                } catch (Exception e) {
                    logger.error(e.toString(), e);
                }
            }
        }
    }

    public static void main(String[] args) throws Exception {
        NettyClient.printLog = false;
        killOld();
        LoaderUtils.loadDirToClasspath(new File(HomePath.getConfPath()));
        SpringApplication.run(ConsoleApp.class, args);
        List<String> allJars = LoaderUtils.getAllJars();
        for (String allJar : allJars) {
            logger.info(allJar);
        }
    }
}
